﻿namespace Crux.Core.SQL.Formatter.Core;
/// <summary>
/// 
/// </summary>
public class Indentation
{
    private enum IndentTypes
    {
        INDENT_TYPE_TOP_LEVEL,
        INDENT_TYPE_BLOCK_LEVEL
    }

    private readonly string indent;
    private readonly Stack<IndentTypes> indentTypes;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="indent"></param>
    public Indentation(string indent)
    {
        this.indent = indent;
        indentTypes = new Stack<IndentTypes>();
    }
    /// <summary>
    /// 
    /// </summary>
    public string GetIndent() =>
        string.Concat(Enumerable.Range(0, indentTypes.Count)
            .Select(_ => indent));
    /// <summary>
    /// 
    /// </summary>
    public void IncreaseTopLevel()
    {
        indentTypes.Push(IndentTypes.INDENT_TYPE_TOP_LEVEL);
    }
    /// <summary>
    /// 
    /// </summary>
    public void IncreaseBlockLevel()
    {
        indentTypes.Push(IndentTypes.INDENT_TYPE_BLOCK_LEVEL);
    }
    /// <summary>
    /// 
    /// </summary>
    public void DecreaseTopLevel()
    {
        if (indentTypes.Count != 0 &&
            indentTypes.Peek() == IndentTypes.INDENT_TYPE_TOP_LEVEL)
        {
            indentTypes.Pop();
        }
    }
    /// <summary>
    /// 
    /// </summary>
    public void DecreaseBlockLevel()
    {
        while (indentTypes.Count > 0)
        {
            var type = indentTypes.Pop();
            if (type != IndentTypes.INDENT_TYPE_TOP_LEVEL)
            {
                break;
            }
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public void ResetIndentation()
    {
        indentTypes.Clear();
    }
}